package com.demomaster.quickcompat.widget.toast

import android.content.Context
import android.os.Build
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.demomaster.quickcompat.R
import com.demomaster.quickcompat.util.QuickThread
import com.demomaster.quickcompat.widget.toast.compat.ToastCompat

object QdToast {
    private var mContext: Context? = null
    @JvmStatic
    fun setContext(context: Context) {
        mContext = context.applicationContext
    }
    @JvmStatic
    @JvmOverloads
    fun show(context: Context? = mContext, obj: Any?, time: Int? = Toast.LENGTH_LONG) {
        if (context == null) return
        if (mContext == null) {
            mContext = context.applicationContext
        }
        if (mContext == null) return
        val duration = time ?: Toast.LENGTH_LONG
        val text: String = if (obj is CharSequence) obj.toString() else obj.toString()
        QuickThread.runOnUiThread {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
                ToastCompat.makeText(mContext, text, duration)
                    .setBadTokenListener { toast ->
                        Log.e("failed toast", text)
                    }.show()
            } else {
                Toast.makeText(mContext, text, duration).show()
            }
        }
    }

    /**************************       自定义toast       *********************************/
    private var customToast: Toast? = null
    @JvmStatic
    fun showToast(context: Context?, msg: String?, duration: Int = Toast.LENGTH_SHORT, toastInflate: QuickCustomToast.ToastInflater? = null) {
        try {
            if (context == null) return
            if (TextUtils.isEmpty(msg)) return
            QuickThread.runOnUiThread {
                customToast?.cancel()
                customToast = QuickCustomToast(context, msg!!, duration, toastInflate)
                customToast?.show()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    //使用自定义toast
    class QuickCustomToast(context: Context, msg: String, duration: Int, toastInflate: ToastInflater?) : Toast(context) {
        private var mToastInflater: ToastInflater = toastInflate ?: DefToastInflater()

        constructor(context: Context, msg: String, duration: Int) : this(context, msg, duration, null)

        init {
            newFtoast(context, msg, duration, toastInflate)
        }

        private fun newFtoast(context: Context, msg: String, duration: Int, toastInflate: ToastInflater?) {
            mToastInflater = toastInflate ?: DefToastInflater()
            val root = mToastInflater.createView(this, context)
            if (root != null) {
                mToastInflater.bindData(this, root, msg)
                setDuration(duration)
                view = root
                setGravity(Gravity.CENTER or Gravity.FILL_HORIZONTAL, 0, 0)
            }
        }

        class DefToastInflater : ToastInflater {
            override fun createView(toast: Toast, context: Context): View {
                val inflate = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
                return inflate.inflate(R.layout.quick_toast, null)
            }

            override fun bindData(toast: Toast, root: View, msg: String) {
                val txtContent = root.findViewById<TextView>(R.id.txtToast)
                txtContent?.text = msg
            }
        }

        interface ToastInflater {
            fun createView(toast: Toast, context: Context): View
            fun bindData(toast: Toast, root: View, msg: String)
        }
    }
}